// const HTTP = "https://reproduce.ascetic.top/api/";
// const HTTP = "https://www.manmiao.online/api/";
// const HTTP = "https://newtsdj.ascetic.top/api/";
const HTTP = "https://apishop.qiancdj.com/api/";

import st from "@/utils/utils/showtoast.js";
import stoarge from "@/utils/utils/stoarge.js";

export const get = (url, data = {}) => {
	return request(url, "GET", data);
};

export const post = (url, data = {}) => {
	return request(url, "POST", data);
};

/**
 * 网络请求
 * @param {String} url 请求地址
 * @param {'GET'|'POST'} method 请求方式
 * @param {Object} data 请求参数
 */
const request = (url, method, data) => {
	if(url !== "service/Technician/LikeTechnician"){
		uni.showLoading({
			title: "请求中",
			mask: true,
		});
	}
	return new Promise((resolve, reject) => {
		let token = stoarge.getToken();
		uni.request({
			url: HTTP + url,
			method,
			data,
			header: {
				"Content-Type": "application/json",
				token,
			},
			success: (res) => {
				uni.hideLoading();
				uni.stopPullDownRefresh();
				let model = res.data;
				if (model.code === 1) {
					resolve(model);
				} else {
					reject(model);
				}
				if (res.data.code == -100) {
					uni.showToast({
						title: '请先登录',
						icon: 'none',
						duration: 2000
					})
					uni.removeStorageSync('___token___');
					setTimeout(() => {
						uni.reLaunch({
							url: '/pages/login/login'
						})
					}, 1000)
				}
			},
			fail: (res) => {
				uni.hideLoading();
				uni.stopPullDownRefresh();
				st.show("网络请求失败，请检查网络连接或稍后重试");
			},
		});
	});
};

export default {
	get,
	post,
	HTTP
};
